package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.EvaluationAdapter;
import com.luck.easyfoodbatch.mvp.model.EvaluateM;
import com.luck.easyfoodbatch.mvp.order.EvaluateListModel;
import com.luck.easyfoodbatch.mvp.order.EvaluateListPresenter;
import com.luck.easyfoodbatch.mvp.order.EvaluateListView;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.verticalslide.CustListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/30
 */
public class EvaluationFragment extends BaseFragment implements EvaluateListView {
    @BindView(R.id.listview)
    CustListView listview;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    private View rootView;
    private List<EvaluateM.DataBean> list = new ArrayList<>();
    private EvaluationAdapter adapter;
    private String gid = "";
    private EvaluateListModel listModel;
    private int page=1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_evaluation, container, false);
            ButterKnife.bind(this, rootView);
            initView();
        }

        return rootView;
    }

    private void initView() {
        gid = getActivity().getIntent().getStringExtra("gid");
        listModel = new EvaluateListPresenter(this, mContext);
        listModel.requestEvaluateList(page+"",gid);
        adapter = new EvaluationAdapter(mContext);
        adapter.setList(list);
        listview.setAdapter(adapter);
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {

                } else if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
                    final View bottomChildView = view.getChildAt(view.getLastVisiblePosition() - view.getFirstVisiblePosition());
                    boolean result = (view.getHeight() >= bottomChildView.getBottom());
                    if (result) {

                    }
                }
            }
        });
    }

    @Override
    public void evaluateListSuccess(EvaluateM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                list = model.getData();
                if (list == null) {
                    list = new ArrayList<>();
                }
                if (list.size() == 0) {
                    iv_nodata.setVisibility(View.VISIBLE);
                } else {
                    iv_nodata.setVisibility(View.INVISIBLE);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                }
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        //hideShowProgress();
    }
}
